﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_MyGrouping : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }
    protected void bnNew_ServerClick(object sender, EventArgs e)
    {
    }
    public void Bind()
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from grouping where username='" + Session["UserName"].ToString() + "'";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
       
    }


   //分页事件
    //protected void PageCut_Click(object sender, EventArgs e)
    //{
    //    string commandArg = ((LinkButton)sender).CommandArgument;
    //    switch (commandArg)
    //    {
    //        case "First":
    //            GridView1.PageIndex = 0;
    //            break;
    //        case "Pre":
    //            GridView1.PageIndex = (int)Math.Max(0, GridView1.PageIndex - 1);
    //            break;
    //        case "Next":
    //            GridView1.PageIndex = (int)Math.Min(GridView1.PageCount - 1, GridView1.PageIndex + 1);
    //            break;
    //        case "Last":
    //            GridView1.PageIndex = GridView1.PageCount - 1;
    //            break;
    //    }
    //    Bind();
    //}
    //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    GridView1.PageIndex = e.NewPageIndex;
    //}
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            
            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string sql = "delete from grouping where  groupingid=" + id + "";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            Response.Write("<script>alert('删除成功!')</script>");
            Bind();
            con.Close();
        }
        catch
        {
         
            Response.Write("<script>alert('对不起,先要删除此组联系人!!')</script>");
        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确认要删除?')");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Response.Redirect("GroupPub.aspx");
    }
}
